<html>
    <head>
        <title>Deferred Directional Light Shadow</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <script src="../dist/claygl.js"></script>
        <script type="text/javascript" src="lib/stats.js"></script>
    </head>
    <body style="margin:0px;">
        <canvas id="main"></canvas>
        <script>
            var renderer = new clay.Renderer({
                canvas: document.getElementById('main'),
                devicePixelRatio: 1.0
            });
            renderer.resize(window.innerWidth, window.innerHeight);
            var shadowMapPass = new clay.prePass.ShadowMap();
            var deferredRenderer = new clay.deferred.Renderer({
                shadowMapPass: shadowMapPass
            });

            var camera = new clay.camera.Perspective({
                far: 1000,
                aspect: renderer.getViewportAspect()
            });

            var scene = new clay.Scene();

            var cubeGeo = new clay.geometry.Cube();

            var cubes = [];

            var CUBE_COUNT = 24;
            for (var i = 0; i < CUBE_COUNT; i++) {
                for (var j = 0; j < CUBE_COUNT; j++) {
                    var mesh = new clay.Mesh({
                        geometry: cubeGeo,
                        material: new clay.StandardMaterial({
                            color: [i / CUBE_COUNT, j / CUBE_COUNT, 0.5]
                        })
                    });
                    mesh.position.x = i - CUBE_COUNT / 2;
                    mesh.position.z = j - CUBE_COUNT / 2;
                    mesh.scale.set(0.5, 0, 0.5);

                    cubes.push(mesh);

                    scene.add(mesh);
                }
            }
            var directionalLight = new clay.light.Directional({
                intensity: 1.2,
                shadowBias: 0.004,
                shadowResolution: 2048,
                shadowCascade: 2
            });
            directionalLight.position.set(10, 10, 10);
            directionalLight.lookAt(scene.position);

            scene.add(directionalLight);

            scene.add(new clay.light.Point({
                intensity: 0.4,
                range: 100,
                position: new clay.Vector3(-10, CUBE_COUNT, 0)
            }));

            camera.position.set(10, 20, 20);
            camera.lookAt(scene.position);

            var control = new clay.plugin.OrbitControl({
                domElement: renderer.canvas,
                target: camera
            });

            var timeline =  new clay.Timeline();

            var elapsedTime = 0;
            timeline.on('frame', function (deltaTime) {
                control.update(deltaTime);

                elapsedTime += deltaTime;
                cubes.forEach(function (cube, idx) {
                    var x = Math.round(idx / CUBE_COUNT);
                    var y = idx % CUBE_COUNT;
                    cube.scale.y = (Math.sin(x / 3 + elapsedTime / 1000) * Math.cos(y / 3 + elapsedTime / 1000) + 1) * 2;
                })
                // shadowMapPass.render(renderer, scene, camera);
                // renderer.render(scene, camera);
                deferredRenderer.render(renderer, scene, camera);

                // shadowMapPass.renderDebug(renderer);

                stats.update();
            });
            timeline.start();

            var stats = new Stats();
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.top = '0px';
            stats.domElement.style.right = '0px';
            document.body.appendChild(stats.domElement);
        </script>
    </body>
</html>